package com.freewater.library.lending.reader.infrastructure.repository;

import com.freewater.library.lending.reader.infrastructure.dao.ReaderDao;
import com.freewater.library.lending.reader.infrastructure.po.ReaderPo;
import com.freewater.library.lending.reader.model.Reader;
import com.freewater.library.lending.reader.model.ReaderIdGenerator;
import com.freewater.library.lending.reader.model.ReaderRepository;
import lombok.RequiredArgsConstructor;

import java.util.Optional;

@RequiredArgsConstructor
public class ReaderRepositoryImpl implements ReaderRepository, ReaderIdGenerator {

    private final ReaderDao readerDao;

    @Override
    public void save(Reader reader) {
        ReaderPo record = new ReaderPo(reader);
        readerDao.save(record);
    }

    @Override
    public Optional<Reader> findById(Long readerId) {
        Optional<ReaderPo> readerOpt = this.readerDao.findById(readerId);
        return readerOpt.map(ReaderPo::convert);
    }

    @Override
    public boolean existsByName(String name) {
        return this.readerDao.existsByName(name);
    }

    @Override
    public Long nextId() {
        return this.readerDao.nextReaderId();
    }
}
